package cn.wildfire.chat.kit.group;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.contact.UserListAdapter;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.contact.pick.CheckableUserListAdapter;
import cn.wildfire.chat.kit.contact.pick.PickUserFragment;
import cn.wildfire.chat.kit.contact.pick.PickUserViewModel;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.GroupMember;

public class PickGroupMemberFragment extends PickUserFragment {
    private GroupInfo groupInfo;

    /**
     * 群主编号
     */
    private String ownerId;

    /**
     * 强制类型显示勾选按钮
     */
    private boolean forceShowSelectorWithNotGroupSet;

    public static PickGroupMemberFragment newInstance(GroupInfo groupInfo) {
        Bundle args = new Bundle();
        args.putParcelable("groupInfo", groupInfo);
        PickGroupMemberFragment fragment = new PickGroupMemberFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static PickGroupMemberFragment newInstance(boolean isNotGroupChoose, GroupInfo groupInfo, String ownerId, boolean forceShowSelectorWithNotGroupSet) {
        Bundle args = new Bundle();
        args.putParcelable("groupInfo", groupInfo);
        args.putBoolean("isNotGroupChoose", isNotGroupChoose);
        args.putBoolean("forceShowSelectorWithNotGroupSet", forceShowSelectorWithNotGroupSet);
        PickGroupMemberFragment fragment = new PickGroupMemberFragment();
        fragment.ownerId = ownerId;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        groupInfo = getArguments().getParcelable("groupInfo");
        forceShowSelectorWithNotGroupSet = getArguments().getBoolean("forceShowSelectorWithNotGroupSet", false);
        if (forceShowSelectorWithNotGroupSet) {
            isNotGroupChoose = false;
        } else {
            isNotGroupChoose = (groupInfo == null);
        }
    }

    @Override
    public void initHeaderViewHolders() {
        // do nothing
    }

    @Override
    public UserListAdapter onCreateUserListAdapter() {
        List<UIUserInfo> usersList = null;
        PickUserViewModel pickUserViewModel = ViewModelProviders.of(getActivity()).get(PickUserViewModel.class);
        CheckableUserListAdapter checkableContactAdapter = new CheckableUserListAdapter(this);
        if (!isNotGroupChoose && !forceShowSelectorWithNotGroupSet) {
            GroupViewModel groupViewModel = ViewModelProviders.of(getActivity()).get(GroupViewModel.class);
            List<GroupMember> members = groupViewModel.getGroupMembers(groupInfo.target, false);
            List<String> memberIds = new ArrayList<>(members.size());
            if (!TextUtils.isTextEmpty(ownerId)) {
                for (GroupMember member : members) {
                    if (!member.memberId.equals(ownerId)) {
                        memberIds.add(member.memberId);
                    }
                }
            } else {
                for (GroupMember member : members) {
                    memberIds.add(member.memberId);
                }
            }
            usersList = userInfoToUIUserInfo(UserViewModel.getUsers(memberIds, groupInfo.target));
            checkableContactAdapter.noselectChecker = false;
        } else {
            if (forceShowSelectorWithNotGroupSet) {
                checkableContactAdapter.noselectChecker = false;
            } else {
                checkableContactAdapter.noselectChecker = true;
            }
            usersList = pickUserViewModel.getUsers();
        }
        pickUserViewModel.setUsers(usersList);
        checkableContactAdapter.setUsers(usersList);
        return checkableContactAdapter;
    }

}
